#include "mainwindow.h"
#include "mybutton.h"
#include "ui_mainwindow.h"

#include <QPainter>
#include <QSound>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置固定大小
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/Coin0001.png"));
    //设置标题
    setWindowTitle("翻金币主场景");

    //退出按钮实现
    connect(ui->actionquit,&QAction::triggered,this,&MainWindow::close);

    QSound * startSound = new QSound(":/TapButtonSound.wav");

    //开始按钮
    MyButton * startBtn = new MyButton(":/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width() * 0.5 - startBtn->width() * 0.5,this->height() * 0.7);

    //实例化选择关卡场景
    chooseScenc = new ChooseLevelScene;

    connect(startBtn,&MyButton::clicked,[=](){

        //播放音效
        startSound->play();

        //qDebug()<<"点击了开始按钮,向下弹跳";
        startBtn->zoom1();
        //qDebug()<<"点击了开始按钮,向上弹跳";
        startBtn->zoom2();

        //延时进入关卡选择界面
        QTimer::singleShot(500,this,[=](){
            //隐藏自身
            this->hide();
            //显示关卡选择场景
            chooseScenc->show();
        });

    });

    //监听选择关卡的返回按钮的信号
    connect(chooseScenc,&ChooseLevelScene::chooseSceneBack,[=](){
        //隐藏自身
        this->show();
        //显示关卡选择场景
        chooseScenc->hide();
    });

}

//重写painterEvent事件，画背景图
void MainWindow::paintEvent(QPaintEvent *){

    QPainter painter(this);
    QPixmap pix;

    //加载背景图片
    pix.load(":/PlayLevelSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载图标
    pix.load(":/Title.png");
    //图标缩放
    pix = pix.scaled(pix.width() * 0.5,pix.height() * 0.5);
    //绘制背景
    painter.drawPixmap(10,30,pix);
}

MainWindow::~MainWindow()
{
    delete ui;
}

